package com.elvt.bolg.web;

import com.wf.captcha.ArithmeticCaptcha;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/captcha")
public class CaptchaServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        ServletOutputStream outputStream = resp.getOutputStream();
        //算术验证码 数字加减乘除. 建议2位运算就行:captcha.setLen(2);
        ArithmeticCaptcha captcha = new ArithmeticCaptcha(120, 40);
        // 中文验证码
//        ChineseCaptcha captcha = new ChineseCaptcha(120, 40);
        // 英文与数字验证码
        // SpecCaptcha captcha = new SpecCaptcha(120, 40);
        //英文与数字动态验证码
        //  GifCaptcha captcha = new GifCaptcha(120, 40);
        // 中文动态验证码
        //ChineseGifCaptcha captcha = new ChineseGifCaptcha(120, 40);
        // 几位数运算，默认是两位
        captcha.setLen(2);
        // 获取运算的结果
        String loginCodeRes = captcha.text();
      //存储在redis中用来验证
        System.out.println("sss"+loginCodeRes);
        req.getSession().setAttribute("loginCodeRes",loginCodeRes);
        captcha.out(outputStream);

    }
}
